<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<div>
				<div class="top_part">
					<p>{{selectBgTask.dutyDay}}，{{selectBgTask.whiteDay =='1'?'白班':'晚班'}} / {{selectBgTask.itemRefName}}
						/ {{selectBgTask.deviceCode}} / {{selectBgTask.planName}} / {{selectBgTask.gradeValue}}</p>
					<div class="_right">
						<div class="text-formList">
							<span class="text_left">申报人：</span>
							<span class="text_right">{{addForm.applyUser}}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">状态：</span>
							<span class="text_right">{{addForm.deduceStatusCn}}</span>
						</div>
					</div>
				</div>
				<div class="tableList">
					<table border="1">
						<tr>
							<th>单件万支</th>
							<th>件数</th>
							<th>产能万支</th>
							<th>工价(元)</th>
							<th>单品薪资</th>
							<th>工序</th>
							<th>姓名</th>
							<th>考勤</th>
							<th>薪资(元)</th>
							<th>补全</th>
							<th>备注</th>
							<th>扣减件数</th>
							<th>单品减扣(元)</th>
							<th>个人减扣(元)</th>
						</tr>
						<tr class="add_Table">
							<td><p>{{ selectBgTask.thNumber }}</p></td>
							<td><p>{{ selectBgTask.pieceNum }}</p></td>
							<td><p>{{ selectBgTask.totalNum }}</p></td>
							<td><p>{{ selectBgTask.gongPrice }}</p></td>
							<td><p>{{ selectBgTask.singlePrice }}</p></td>
							<td>
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.gongxu}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.userName}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.kaoQin}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.userPrice}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.buQuan}}</span>
								</p>
							</td>
							<td><p>{{ selectBgTask.remark }}</p></td>
							<td>
								<el-input
									@input="selectBgTask.deleteCount=selectBgTask.deleteCount.replace(/[^\d]/g,'')"
									v-model="selectBgTask.deleteCount" placeholder="" class="text_center" clearable
									:disabled="type == 'see'" @change="changeDeleteCount"></el-input>
							</td>
							<td>
								<el-input v-model="selectBgTask.singleDelete" placeholder="" class="text_center" clearable disabled></el-input>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<el-input
										@input="item2.deducePrice=item2.deducePrice.replace(/[^\d]/g,'')"
										v-model="item2.deducePrice" placeholder="" class="text_center" clearable
										:disabled="type == 'see'"></el-input>
								</p>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
	export default {
		props: ['type', 'editData','singlePeople'],
		data() {
			return {
				queryForm: {
					dutyDay: '',
					itemId: '',
				},
				itemList: ['单支连排', '多支', '伸缩', 'U型'],
				addForm: {id:'',bgId:''},
				selectBgTask: {deleteCount: ''},
				currentUserId:'',
				count: 0,
				user:{},
			}
		},

		methods: {

			changeDeleteCount(){
				this.selectBgTask.singleDelete = '';
				if(!this.selectBgTask.deleteCount){
					return;
				}
				let thNumber = this.selectBgTask.thNumber;
				let deleteCount = this.selectBgTask.deleteCount;
				let gongPrice = this.selectBgTask.gongPrice;
				let singleDelete = parseFloat(gongPrice) * deleteCount *  parseFloat(thNumber);
				this.selectBgTask.singleDelete = singleDelete;
			},

			getOnDutyBgTaskWithUserListById() {
				let self = this;
				self.$http({
					url: "/jinl/bgTask/getOnDutyBgTaskWithUserListById",
					method: "post",
					params: {id:self.addForm.bgId}
				}).then(resp => {
					self.tableLoading = false;
					if (resp.success) {
						self.selectBgTask= resp.result;
						console.log('==self.selectBgTask===')
						console.log(self.selectBgTask)
						self.$set(self.selectBgTask,'deleteCount',self.addForm.deleteCount);
						let pieceNum = self.selectBgTask.pieceNum;
						let thNumber = self.selectBgTask.thNumber;
						let totalNum = parseFloat(pieceNum) * parseFloat(thNumber);
						self.selectBgTask.totalNum = totalNum.toFixed(2);

						let deleteCount = self.selectBgTask.deleteCount;
						let gongPrice = self.selectBgTask.gongPrice;
						let singleDelete = parseFloat(gongPrice) * deleteCount *  parseFloat(thNumber);
						self.$set(self.selectBgTask,'singleDelete',singleDelete.toFixed(2));
						let userCount = self.selectBgTask.onDutyBgTaskUsers.length;
						let deducePrice = singleDelete/userCount;
						let chooseUser = {};
						for(let data of self.selectBgTask.onDutyBgTaskUsers){
							if(!data.hasOwnProperty('deducePrice')){
								self.$set(data,'deducePrice',deducePrice);
							}
							if(data.userId == self.currentUserId ){
								chooseUser = Object.assign({},data);
							}
						}
						if(self.singlePeople == '1'){
							chooseUser.deducePrice = this.addForm.singleReducePrice;
							let onDutyBgTaskUsers = [chooseUser];
							self.selectBgTask.onDutyBgTaskUsers = onDutyBgTaskUsers;
						}
					}
				});
			},

			submitForm() {
				let self = this;
				if (!self.selectBgTask.deleteCount) {
					self.$message.warning('请填写扣减件数');
					return;
				}
				let total = 0.0;
				let onDutyReducePeoples = [];
				for(let data of self.selectBgTask.onDutyBgTaskUsers){
					if(!data.deducePrice){
						self.$message.warning('请填写个人扣减');
						return;
					}
					let people = {reduceId:self.addForm.id,userId:data.userId,userName:data.userName,reducePrice:data.deducePrice};
					total +=parseFloat(data.deducePrice);
					onDutyReducePeoples.push(people);
				}
				if(total != parseFloat(self.selectBgTask.singleDelete)){
					self.$message.warning('个人减扣总额不等于单品减扣金额！');
					return;
				}
				if (self.count == 0) {
					++self.count;
					let params = {deducePrice:total,onDutyReducePeoples:onDutyReducePeoples,id:self.addForm.id,checkUser:self.user.userName,bgId:self.selectBgTask.id,deleteCount:self.selectBgTask.deleteCount};
					self.$http({
						url: "/jinl/reduce/updateCheckOnDutyReduce",
						method: "post",
						data: JSON.stringify(params),
						dataType: 'json',
						contentType: 'application/json',
					}).then(resp => {
						self.count = 0;
						if (resp.success) {
							self.$eventBus.$emit('updateOndutyCheckList');
							self.$eventBus.$emit('getOnDutyReduceCount');
							return;
						}
						self.$message.error(resp.message)
					});
				}
			},
		},
		mounted() {

		},
		watch:{
			editData(val) {
				this.addForm = Object.assign({},val);
				this.currentUserId = this.addForm.userId;
				console.log('this.currentUserId')
				console.log(this.currentUserId)
				this.user = getStore();
				this.getOnDutyBgTaskWithUserListById();
			},
		},
		components: {}
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.business-workAdd.addProject {
		.top_part {
			display: flex;
			justify-content: space-between;

			p {
				line-height: 32px;
			}

			._right {
				display: flex;

				.text-formList {
					margin-left: 15px;
					width: 140px;

					// .text_right {
					//     color: #2196f3;
					// }
				}
			}
		}

		.Search_Top_Input {
			.search_list {
				display: flex;
			}

			.zll-botton {
				height: 26px;
				line-height: 26px;
				padding: 0 15px;
				margin: 0;
			}
		}

		.formList {
			height: 32px;
		}

		.tableList {
			margin-bottom: 20px;

			table {
				tr {
					background: #fff !important;

					td {
						padding: 1px 3px;
					}
				}
			}
		}
	}
</style>